// pages/feedback/index.js
const {
  uploadPic,
  uploadImage,
  uploadVideo,
  uploadStaticFile
} = require('../../../utils/util')
const {
  flowGetById,
  abnormalGetById,
  abnormalApiGetById,
  dealAbnormal,
  dealAbnormalApi,
  flowRemind,
  flowAuditCreate
} = require('../../../api/api')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    loginVisible: true,
    showSelect: false,
    type: null,
    queIndex: null,
    picsMap: [],
    photoList: [],
    fileList: [],
    picsMap2: [],
    photoList2: [],
    fileList2: [],
    picsMap3: [],
    photoList3: [],
    fileList3: [],

    picsMap4: [],
    photoList4: [],
    fileList4: [],
    maxLength: 9,
    bottomLift: getApp().globalData.bottomLift,
    status: 0,
    auditStatus: 1,
    id: null,
    info: {},
    description: '',
    isSuccess: false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if (options.id) {
      this.setData({
        id: options.id,
        status: parseInt(options.status)
      })
      this.checkStatus()
    }
    wx.hideHomeButton();
  },
  checkStatus() {
    const {
      status
    } = this.data
    if (status === 0) {
      this.loadData()
    } else {
      this.loadAbnorlmalData()
    }
  },
  async loadAbnorlmalData() {
    const {
      id,
      picsMap,
      picsMap2,
      picsMap3
    } = this.data
    const params = {
      id
    }
    const result = await abnormalApiGetById(params)
    const fileData = [],
      fileData2 = [],
      fileData3 = [];
    if (result.auditDTO) {
      if (result.auditDTO.fileList && result.auditDTO.fileList.length > 0) {
        for (let i = 0; i < result.auditDTO.fileList.length; i++) {
          var file = result.auditDTO.fileList[i];
          picsMap.push({
            'url': file.url,
            'id': file.id
          })
          fileData.push(file.url);
        }
      }
      if (result.auditDTO.fileList2 && result.auditDTO.fileList2.length > 0) {
        for (let i = 0; i < result.auditDTO.fileList2.length; i++) {
          var file = result.auditDTO.fileList2[i];
          picsMap2.push({
            'url': file.url,
            'id': file.id
          })
          fileData2.push(file.url);
        }
      }
      if (result.auditDTO.fileList3 && result.auditDTO.fileList3.length > 0) {
        for (let i = 0; i < result.auditDTO.fileList3.length; i++) {
          var file = result.auditDTO.fileList3[i];
          picsMap3.push({
            'url': file.url,
            'id': file.id
          })
          fileData3.push(file.url);
        }
      }
    }
    this.setData({
      info: result,
      description: result.description,
      feedback: result.feedback,
      fileList: fileData,
      fileList2: fileData2,
      fileList3: fileData3
    })
  },
  async loadData() {
    const {
      id
    } = this.data
    const params = {
      id
    }
    const result = await flowGetById(params)
    this.setData({
      info: result
    })
  },
  chooseImage(e) {
    var that = this;
    const type = e.currentTarget.dataset.type
    var {
      picsMap,
      maxLength,
      fileList
    } = this.data
    var count = maxLength - picsMap.length
    uploadImage(count).then((filePath) => {
      that.setData({
        photoList: filePath,
        fileList: fileList.concat(filePath)
      })
      that.upload()
    })
  },
  chooseImage2(e) {
    var that = this;
    const type = e.currentTarget.dataset.type
    var {
      picsMap2,
      maxLength,
      fileList2
    } = this.data
    var count = maxLength - picsMap2.length
    uploadImage(count).then((filePath) => {
      that.setData({
        photoList2: filePath,
        fileList2: fileList2.concat(filePath)
      })
      that.upload2()
    })
  },
  chooseImage3(e) {
    var that = this;
    const type = e.currentTarget.dataset.type
    var {
      picsMap3,
      maxLength,
      fileList3
    } = this.data
    var count = maxLength - picsMap3.length
    uploadImage(count).then((filePath) => {
      that.setData({
        photoList3: filePath,
        fileList3: fileList3.concat(filePath)
      })
      that.upload3()
    })
  },
  chooseVideo(e) {
    var that = this;
    const type = e.currentTarget.dataset.type
    var {
      picsMap4,
      maxLength,
      fileList4
    } = this.data
    var count = maxLength - picsMap4.length
    uploadVideo(count).then((filePath) => {
      that.setData({
        photoList4: filePath,
        fileList4: fileList4.concat(filePath)
      })
      console.log('++++' + filePath)
      that.upload4()
    })
  },
  hideSuccess() {
    this.setData({
      isSuccess: false,
    })
  },
  goback(e) {
    wx.navigateBack();
  },
  onRadioChange(e) {
    // console.log('radio发生change事件，携带value值为：', e.detail.value)
    this.setData({
      auditStatus: parseInt(e.detail.value)
    });
  },
  async submit(e) {
    const params = e.detail.value;
    const {
        id,auditStatus
      } = this.data
    if(auditStatus === 3){//提醒经销商
      params.documentId = id;
      const result = await flowRemind(params)
      // console.log("result:"+JSON.stringify(result));
      console.log(result);
      this.setData({
        isSuccess: true,
      })
  
        // wx.showToast({
        //   title: "已下发提醒！",
        //   icon: 'none'
        // })
        return;
    }
    const {
      picsMap,picsMap2,picsMap3
    } = this.data
    params.documentId = id;
    params.type='flow';
    params.auditState = auditStatus;
    params.trackList = picsMap;//轨迹图
    params.sceneList = picsMap2;//现场图
    params.documentList = picsMap3;//单据图
    const result = await flowAuditCreate(params);
    console.log(result);
    this.setData({
      isSuccess: true,
    })

    // wx.showToast({
    //   title: '处理成功',
    //   icon: 'none'
    // })
    // this.checkStatus()
    // setTimeout(() => {
    //   wx.navigateBack({
    //     delta: 1,
    //   })
    // }, 500);
    // const pages=getCurrentPages()
    // const prevPage=pages[pages.length-2]
    // prevPage.setData({
    //   list:[],
    //   page:1
    // })
    // prevPage.onLoad()
  },
  async upload() {
    var _this = this
    const {
      picsMap,
      photoList
    } = this.data;

    for (let i = 0; i < photoList.length; i++) {
      const img = await uploadStaticFile(photoList[i])
      picsMap.push(img)
    }
    _this.setData({
      picsMap,
      images: [],
      photolist: []
    })
    wx.hideLoading()
  },
  async upload2() {
    var _this = this
    const {
      picsMap2,
      photoList2
    } = this.data;
    for (let i = 0; i < photoList2.length; i++) {
      const img = await uploadStaticFile(photoList2[i])
      picsMap2.push(img)
    }
    _this.setData({
      picsMap2,
      images: [],
      photolist2: []
    })
    wx.hideLoading()
  },
  async upload3() {
    var _this = this
    const {
      picsMap3,
      photoList3
    } = this.data;

    for (let i = 0; i < photoList3.length; i++) {
      const img = await uploadStaticFile(photoList3[i])
      picsMap3.push(img)
    }
    _this.setData({
      picsMap3,
      images: [],
      photolist3: []
    })
    wx.hideLoading()
  },
  async upload4() {
    var _this = this
    const {
      picsMap4,
      photoList4
    } = this.data;
    const img = await uploadStaticFile(photoList4)
    picsMap4.push(img)
    _this.setData({
      picsMap4,
      images: [],
      photolist4: []
    })
    wx.hideLoading()
  },
  previewImg(e) {
    getApp().globalData.preview = false;
    var src = e.currentTarget.dataset.url //获取data-src  循环单个图片链接
    // var str = this.data.details.imgList; //获取data-effect_pic   图片列表
    var str = this.data.fileList
    //图片预览
    wx.previewImage({
      current: src,
      // 当前显示图片的http链接
      urls: str // 需要预览的图片http链接列表
    });
  },
  delPhoto(e) {
    const picsMap = this.data.picsMap;
    const fileData = [];
    if (picsMap.length) {
      const id = e.currentTarget.dataset.id;
      picsMap.splice(id, 1);
      for (let i = 0; i < picsMap.length; i++) {
        var file = picsMap[i];
        fileData.push(file.url);
      }
      this.setData({
        picsMap,
        fileList: fileData
      })
    }
  },
  delPhoto2(e) {
    const picsMap = this.data.picsMap2;
    const fileData = [];
    if (picsMap.length) {
      const id = e.currentTarget.dataset.id;
      picsMap.splice(id, 1);
      for (let i = 0; i < picsMap.length; i++) {
        var file = picsMap[i];
        fileData.push(file.url);
      }
      this.setData({
        picsMap2: picsMap,
        fileList2: fileData
      })
    }
  },
  delPhoto3(e) {
    const picsMap = this.data.picsMap3;
    const fileData = [];
    if (picsMap.length) {
      const id = e.currentTarget.dataset.id;
      picsMap.splice(id, 1);
      for (let i = 0; i < picsMap.length; i++) {
        var file = picsMap[i];
        fileData.push(file.url);
      }
      this.setData({
        picsMap3: picsMap,
        fileList3: fileData
      })
    }
  },
  delPhoto4(e) {
    const picsMap = this.data.picsMap4;
    const fileData = [];
    if (picsMap.length) {
      const id = e.currentTarget.dataset.id;
      picsMap.splice(id, 1);
      for (let i = 0; i < picsMap.length; i++) {
        var file = picsMap[i];
        fileData.push(file.url);
      }
      this.setData({
        picsMap4: picsMap,
        fileList4: fileData
      })
    }
  },
})